package cn.jaychang.demo5.order.controller;

import cn.jaychang.demo5.common.model.LoginUser;
import cn.jaychang.demo5.common.model.Result;
import cn.jaychang.demo5.common.model.order.Order;
import cn.jaychang.demo5.common.model.product.Product;
import cn.jaychang.demo5.common.util.OAuthUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    //TODO 为了演示构造两个订单
    public static List<Order> orders=new ArrayList<>();

    static{
        Order order1 = Order.builder()
                .id(1L)
                .useMoney(10000L)
                .build();
        Order order2 = Order.builder()
                .id(2L)
                .useMoney(10000L)
                .build();

        orders.add(order1);
        orders.add(order2);
    }


    @SentinelResource("loginInfo")
    @RequestMapping("/login/info")
    public LoginUser info(){
        return OAuthUtil.getCurrentUser();
    }

    @RequestMapping("/login/admin")
    public LoginUser admin(){
        return OAuthUtil.getCurrentUser();
    }

    @RequestMapping("/info")
    public Result info(@RequestParam(value = "orderId") Long orderId){
        List<Product> list = LongStream.of(1L, 2L, 3L).mapToObj(p -> Product.builder().id(p).name("码猿技术专栏").money(10000L).build()).collect(Collectors.toList());
        Order order = Order.builder()
                .id(orderId)
                .useMoney(10000L)
                .products(list)
                .build();
        return new Result(200,"请求成功!",order);
    }

    @PostMapping("/listByUserId")
    public Result listByUserId(){
        //获取用户的userId
        String userId = OAuthUtil.getCurrentUser().getUserId();
        log.info("当前登录用户的userId：{}",userId);

        //TODO 为了演示不从数据库查询了
        for (Order order : orders) {
            List<Product> list = LongStream.of(1L, 2L, 3L).mapToObj(p -> Product.builder().id(p).name("码猿技术专栏").money(10000L).build()).collect(Collectors.toList());
            order.setProducts(list);
        }
        return new Result(200,"请求成功",orders);
    }
}
